﻿Imports System.IO

Public Class FileMenu
    Public Sub FileMenu_DropDown(ByVal sender As Object, ByVal e As EventArgs)
        ' Enable Project saving only if a project is open
        If IsNothing(Globals.CurrentProject) Then
            MainForm.ProjectSaveMenuItem.Enabled = False
        Else
            MainForm.ProjectSaveMenuItem.Enabled = True
        End If

        ' Temporarily disable new and open until projectless editing is supporting
        MainForm.NewToolStripMenuItem.Enabled = Not IsNothing(Globals.CurrentProject)
        MainForm.OpenToolStripMenuItem.Enabled = Not IsNothing(Globals.CurrentProject)

        MainForm.SaveToolStripMenuItem.Enabled = Not IsNothing(MainForm.EditorTabManager.CurrentTab)
        MainForm.SaveAsToolStripMenuItem.Enabled = Not IsNothing(MainForm.EditorTabManager.CurrentTab)
        MainForm.SaveAllToolStripMenuItem.Enabled = Not IsNothing(MainForm.EditorTabManager.CurrentTab)

        MainForm.PrintToolStripMenuItem.Enabled = Not IsNothing(MainForm.EditorTabManager.CurrentTab)
        MainForm.PrintPreviewToolStripMenuItem.Enabled = Not IsNothing(MainForm.EditorTabManager.CurrentTab)
    End Sub

    Public Sub ProjectNewToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        NewProjectForm.Show()
    End Sub

    Public Sub ProjectOpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim opendialog As New OpenFileDialog()
        opendialog.Title = "Project Open"
        Dim result = opendialog.ShowDialog()

        If result = DialogResult.OK Then
            If File.Exists(opendialog.FileName) Then
                Globals.CurrentProject = ProjectManager.OpenProject(opendialog.FileName)
            End If
        End If
    End Sub

    Public Sub ProjectSaveToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsNothing(Globals.CurrentProject) Then
            Globals.CurrentProject.SaveProject()
        End If
    End Sub

    Public Sub NewToolStripButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ProjectNewFileDialog.ShowDialog()
    End Sub

    Public Sub OpenToolStripButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Can only open project files for now...
        If Not IsNothing(Globals.CurrentProject) Then
            Dim opendir As New OpenFileDialog()
            opendir.Title = "Open File"
            opendir.InitialDirectory = Globals.CurrentProject.DevelopmentFolder
            opendir.Filter = "UnrealScript (*.uc) |*.uc"
            opendir.CheckPathExists = True

            ' Open the file in a tab
            Dim result = opendir.ShowDialog()
            If result = DialogResult.OK Then
                If Globals.CurrentProject.FileList.IsProjectFile(opendir.FileName) Then
                    Dim projfile = Globals.CurrentProject.FileList.GetProjectFile(opendir.FileName)
                    MainForm.EditorTabManager.AddTab(projfile.FileName, projfile)
                End If
            End If
        End If
    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.SaveCurrentTab()
    End Sub

    Public Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.SaveCurrentTab()
    End Sub

    Public Sub SaveAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MainForm.EditorTabManager.SaveAllTabs()
    End Sub

    Public Sub PrintToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsNothing(MainForm.EditorTabManager.CurrentTab) Then
            MainForm.EditorTabManager.CurrentTab.ScintillaEditor.Printing.Print()
        End If
    End Sub

    Public Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsNothing(MainForm.EditorTabManager.CurrentTab) Then
            MainForm.EditorTabManager.CurrentTab.ScintillaEditor.Printing.PrintPreview()
        End If
    End Sub

    Public Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub
End Class
